from data import Datagrama
from data.Datagrama import SimpleDatagram, IPDatagram
from engine.Assembler import mountSimpleDatagram
import re
import string
import sys

def getLine(line):
    return re.split(" ", line )

# Retorna o ip e a porta
def getIpPort(data):
    s_port = re.split("\.", data)
    s_port = s_port[len(s_port)-1]
    return data[0:len(data)-len(s_port)] , s_port


def protocolStatistcs(col,output):
    protos = []
    count = []
    
    for i in col:
        if i.layer in protos:
            count[protos.index(i.layer)] += 1
        else:
            protos.append(i.layer)
            count.append(1)
    
    print "By Protocol"
    csvFile = str(output) + "-Layer.csv"
    csvFile = open(csvFile,"wt")
    
    for i in range(len(protos)):
        p = ((count[i]/float(len(col)))*100)
        print protos[i] , " : packets number = " , count[i] , " : " , p , "%"
        csvFile.write(str(protos[i]) +","+str(count[i])+","+str(p) +"%\n")
    print "\n"
    csvFile.close()

def dstPortStatistcs(col,output):
    ports , count = getImportantPorts()
    transport = [0,0] 
    
    for i in col:
        if  i.layer == "IP":
            if i.port_dst in ports or i.port_origin in ports:
#                print i.port_dst ,i.port_origin 
                if i.port_origin in ports:
                    count[ports.index(i.port_origin)] += 1                     
                else:
                    count[ports.index(i.port_dst)] += 1                
            else:
                ports.append(i.port_dst)
                count.append(1)
            
            if i.transport == "TCP":
                transport[0] += 1
            else:
                transport[1] += 1
    
    print "TCP/UDP"
    
    csvFile = str(output) + "-TCP-UDP.csv"
    csvFile = open(csvFile , "wt")
    csvFile.write("TCP,"+str(transport[0]) + "," + str((transport[0]/float(len(col)))*100)+"%\n")
    csvFile.write("UDP,"+str(transport[0]) + "," + str((transport[0]/float(len(col)))*100)+"%\n")
    csvFile.close()
    
    print "TCP : " + str(transport[0]) + "packets  , " + str((transport[0]/float(len(col)))*100) , "%"
    print "UDP : " + str(transport[1]) + "packets  , " + str((transport[1]/float(len(col)))*100) , "%"
    
    print "By Port"
    csvFile = str(output) + "-TCP-byPort.csv"
    csvFile = open(csvFile,"wt")
    csvFile.write("PORT,PACKET COUNT,%")
    for i in range(len(ports)):
        print ports[i] , " : packets number = " , count[i] , " : " , ((count[i]/float(len(col)))*100) , "%"
        op =  str(ports[i])+","+str(count[i])+","+str((count[i]/float(len(col)))*100)+"\n"
        csvFile.write(str(op))
    
    csvFile.close()
    print "\n"
        
def getImportantPorts():
    ports = [str(80),str(8080),str(137),str(138),str(139),str(53),str(3360),str(22),str(2220),str(1863),str(443)]
    counts = [0,0,0,0,0,0,0,0,0,0,0]
    return ports,counts

def usage():
    print "annalyzer <input file> <output_file>"

if __name__ == '__main__':
    
    if len(sys.argv) < 3:
        usage()
        sys.exit(0)

    file = sys.argv[1]
    output = sys.argv[2]
    f = open(file,"r")

    data = []
    for line in f:
        if line == "\n":
            print "Linha em branco"
        else:
#            print getLine(line)
            data.append(mountSimpleDatagram(line))
    

    
    protocolStatistcs(data,output)
    dstPortStatistcs(data,output)